package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.domain.ArticleListBean;
import com.dmooo.yxmmerchant.domain.ArticleMsgBean;
import com.dmooo.yxmmerchant.view.contract.NewsContract;
import com.dmooo.yxmmerchant.view.model.NewsModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

public class NewsPresetener extends BasePresenter<NewsContract.NewsView> implements NewsContract.NewsPtr {
    private NewsModel model;
    GsonBuilder builder=new GsonBuilder();
    public NewsPresetener(NewsContract.NewsView mView, Context context) {
        super(mView);
        model=new NewsModel(context);
        builder.serializeNulls();
    }

    @Override
    public void getNewsDetail(String article_id) {
        model.getNewDetail(article_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                try {
                    JSONObject object=new JSONObject(builder.create().toJson(o)).getJSONObject("article_msg");
                    mView.showNewDetail(builder.create().fromJson(object.toString(),ArticleMsgBean.class));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getListByCatId(String cat_id, int page) {
        model.getListByCatId(cat_id, page, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsafds",o.toString());
                try {
                    mView.showArticleList(new Gson().fromJson(new JSONObject(builder.create().toJson(o)).toString(),ArticleListBean.class).list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }

            }

            @Override
            public void onFail(Object o) {

            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
